﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Jidong
{
    public partial class 设备报废申请 : System.Web.UI.Page
    {
        private void ShowMessage(string msg)
        {
            this.Panel_Msg.Visible = true;
            this.Message.Text = "·" + msg;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.Date3.Text = DateTime.Today.ToLongDateString();
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }
                if (id > 0)
                {
                    this.BillID.Text = id.ToString();
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = id.ToString();
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
            }
         }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (e.Command.Parameters[1].Value == DBNull.Value)
            {
                return;
            }
            this.BianHao0.Text = (string)e.Command.Parameters[1].Value;
            this.MingCheng.Text = (string)e.Command.Parameters[2].Value;
            this.XingHao.Text = (string)e.Command.Parameters[3].Value;

            this.YuanZhi.Text = ((decimal)e.Command.Parameters[4].Value).ToString("0.00");

            if (e.Command.Parameters[5].Value != DBNull.Value)
            {
                this.Date1.Text = ((DateTime)e.Command.Parameters[5].Value).ToLongDateString();
                this.YiYongNian.Text = (DateTime.Today - (DateTime)e.Command.Parameters[5].Value).Days.ToString();
            }

            this.NianXian.Text = ((short)e.Command.Parameters[6].Value).ToString();
            this.ChuChangBH.Text = (string)e.Command.Parameters[7].Value;

            if (e.Command.Parameters[8].Value != DBNull.Value)
            {
                //this.Date2.Text = ((DateTime)e.Command.Parameters[8].Value).ToLongDateString();
                this.Date2.Text = (string)e.Command.Parameters[8].Value;
            }
            this.DiDian.Text = (string)e.Command.Parameters[9].Value;


            if (e.Command.Parameters[10].Value == DBNull.Value)
            {
                return;
            }
            
            this.BaoFeiID.Text = ((int)e.Command.Parameters[10].Value).ToString();

            this.Date3.Text = ((DateTime)e.Command.Parameters[11].Value).ToLongDateString();
            this.JingBanRen.Text = (string)e.Command.Parameters[12].Value;
            this.MoSun.Text = (string)e.Command.Parameters[13].Value;
            this.HuiShou.Text = (string)e.Command.Parameters[14].Value;
            this.ZheJia.Text = ((decimal)e.Command.Parameters[15].Value).ToString("0.00");
            this.YuanYin.Text = (string)e.Command.Parameters[16].Value;
            this.JianDing.Text = (string)e.Command.Parameters[17].Value;
            this.ShenPi.Text = (string)e.Command.Parameters[18].Value;
            this.BeiZhu.Text = (string)e.Command.Parameters[19].Value;
            this.Button_Save.Text = "保 存";
            this.Button_Save.OnClientClick = "";
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            bool n = false;
            if (this.BaoFeiID.Text == "0")
            {
                n = true;
            }
            DateTime d;
            if (!DateTime.TryParse(this.Date3.Text, out d))
            {
                this.ShowMessage("报废日期录入有误");
                return;
            }
            decimal c;
            if (this.ZheJia.Text.Trim() != "")
            {
                if (!decimal.TryParse(this.ZheJia.Text, out c))
                {
                    this.ShowMessage("变价收入录入有误");
                    return;
                }
            }
            this.SqlDataSource1.UpdateParameters["ID"].DefaultValue = this.BillID.Text;
            this.SqlDataSource1.UpdateParameters["报废ID"].DefaultValue = this.BaoFeiID.Text;
            this.SqlDataSource1.UpdateParameters["报废日期"].DefaultValue = d.ToShortDateString();
            this.SqlDataSource1.UpdateParameters["经办人"].DefaultValue = this.JingBanRen.Text;
            this.SqlDataSource1.UpdateParameters["磨损"].DefaultValue = this.MoSun.Text;
            this.SqlDataSource1.UpdateParameters["回收"].DefaultValue = this.HuiShou.Text;
            this.SqlDataSource1.UpdateParameters["折价"].DefaultValue = this.ZheJia.Text;
            this.SqlDataSource1.UpdateParameters["原因"].DefaultValue = this.YuanYin.Text;
            this.SqlDataSource1.UpdateParameters["鉴定意见"].DefaultValue = this.JianDing.Text;
            this.SqlDataSource1.UpdateParameters["审批意见"].DefaultValue = this.ShenPi.Text;
            this.SqlDataSource1.UpdateParameters["备注"].DefaultValue = this.BeiZhu.Text;

            if (this.SqlDataSource1.Update() > 0)
            {
                if (n)
                    this.ShowMessage("报废操作成功");
                else
                    this.ShowMessage("保存成功");
            }
            else
            {
                this.ShowMessage("操作失败");
            }
        }

        protected void SqlDataSource1_Updated(object sender, SqlDataSourceStatusEventArgs e)
        {
            this.BaoFeiID.Text = ((int)e.Command.Parameters[1].Value).ToString();
            this.Button_Save.Text = "保 存";
            this.Button_Save.OnClientClick = "";
        }
    }
}
